(function() {
  'use strict';

  angular.module('starter.pages.plan.controller', [])
    .controller('PlanCtrl', function($scope, $ionicModal, $ionicListDelegate, Invites, Plans) {

      $scope.data = {
        showDelete: false
      };

      $scope.plans = Plans.all();

      $scope.invites = Invites.all();

      $ionicModal.fromTemplateUrl('templates/dialog-invite.html', {
        scope: $scope
      }).then(function(modal) {
        $scope.modal = modal;
      });

      $scope.closeInvite = function() {
        $scope.modal.hide();
      };

      $scope.doInvite = function() {
        console.log('Doing login', $scope.loginData);

        // Simulate a login delay. Remove this and replace with your login
        // code if using a login system
        $timeout(function() {
          $scope.closeInvite();
        }, 1000);
      };

      $scope.items = ['项目一', '项目二', '项目三', '项目四'];
      $scope.onInvite = function() {
        $scope.modal.show();
      };

      $scope.onDeletePlan = function(plan) {
        Plans.remove(plan);
        $ionicListDelegate.closeOptionButtons();
      };

      $scope.doRefresh = function() {
        $scope.$broadcast('scroll.refreshComplete');
      };

    })
    .controller('PlanAddCtrl', function($scope, $rootScope, Plans, OnePlans) {

      $scope.plans = Plans.all();
      $scope.plan = OnePlans.next();

      $scope.initAddPlan = function() {
        $rootScope.doState.isModify = false;
      };

      /**
       * 添加一个空的子计划
       */
      $scope.doAddChildPlan = function() {
        $rootScope.newPlanId -= 1;
        OnePlans.addChild({
          id: $rootScope.newPlanId,
          name: '点击修改',
          hours: 8,
          status: 0
        });
      };

      /**
       * 删除子计划
       * @param plan 子计划
       */
      $scope.doDeletePlan = function(plan) {
        OnePlans.removeChild(plan);
      };


      var watch = $scope.$watch(function() {
        return $scope.plan;
      }, function(newValue, oldValue, scope) {

        var eq = angular.equals(newValue, oldValue);
        if (!eq) {

          $rootScope.doState.isModify = true;

          $rootScope.doState.afterCall = function() {
            Plans.add($scope.plan);
          };

        }

      }, true);

      $scope.$on('$destroy', function() {
        watch();
        watch = null;
      });

    })
    .controller('PlanEditCtrl', function($scope, $rootScope, $stateParams, Plans, OnePlans) {

      $scope.plan = Plans.get($stateParams.planId);

      OnePlans.set($scope.plan);

      $scope.initAddPlan = function() {
        $rootScope.doState.isModify = false;
      };

      /**
       * 删除子计划
       * @param plan 子计划
       */
      $scope.doDeletePlan = function(plan) {
        OnePlans.removeChild(plan);
      };

      var watch = $scope.$watch(function() {
        return $scope.plan;
      }, function(newValue, oldValue, scope) {

        var eq = angular.equals(newValue, oldValue);
        if (!eq) {

          $rootScope.doState.isModify = true;

          $rootScope.doState.afterCall = function() {
            Plans.add($scope.plan);
          };

        }

      }, true);

      $scope.$on('$destroy', function() {
        watch();
        watch = null;
      });

    })
    .controller('PlanChildListCtrl', function($scope, $rootScope, $stateParams, $ionicModal, OnePlans) {

      $scope.plan = OnePlans.get();
      $scope.plans = $scope.plan.childPlans;

      $ionicModal.fromTemplateUrl('templates/plan/dialog-child-plan-edit.html', {
        scope: $scope
      }).then(function(modal) {
        $scope.modal = modal;
      });

      $scope.closeDialog = function() {
        $scope.modal.hide();
      };

      $scope.onNewChildPlan = function() {

        $rootScope.newPlanId -= 1;

        $scope.childPlan = {
          id: $rootScope.newPlanId,
          name: '',
          hours: 8,
          content: '',
          status: 0
        };

        $scope.modal.show();

      };

      $scope.onEditChildPlan = function(cplan) {
        $scope.childPlan = cplan;
        $scope.modal.show();
      };

      $scope.doSaveChildPlan = function() {
        OnePlans.addChild($scope.childPlan);
        $scope.closeDialog();
      };

      $scope.onDeleteChildPlan = function(childPlan) {
        OnePlans.removeChild(childPlan);
      };

    })
    .controller('PlanContactsListCtrl', function($scope, $stateParams, $ionicModal, OnePlans) {

      $scope.plan = OnePlans.get();
      $scope.contacts = $scope.plan.contacts;

      $scope.remove = function(chat) {

        OnePlans.removeContact(chat);

      };

      $ionicModal.fromTemplateUrl('templates/plan/dialog-contacts-search.html', {
        scope: $scope
      }).then(function(modal) {
        $scope.searchModal = modal;
      });

      $scope.onNewContacts = function() {
        $scope.searchModal.show();
      };

      $scope.closeSearchModal = function() {
        $scope.searchModal.hide();
      };

      $scope.saveSearchModal = function() {



        $scope.closeSearchModal();
      };


      $ionicModal.fromTemplateUrl('templates/plan/dialog-contacts-import.html', {
        scope: $scope
      }).then(function(modal) {
        $scope.importModal = modal;
      });

      $scope.onImportContacts = function() {
        $scope.importModal.show();
      };
      $scope.closeImportModal = function() {
        $scope.importModal.hide();
      };

      $scope.saveImportModal = function() {



        $scope.closeImportModal();
      };

    });


})();
